package com.boe.hbd.client.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.util.Log;

public final class Utils {
    private final static double EARTH_RADIUS_IN_METERS =  6378.137 ;
    
	public static void textDialog(Context context, String title, String text) {
		new AlertDialog.Builder(context).setTitle(title).setMessage(text).setNeutralButton("OK", new DialogInterface.OnClickListener() {
			// @Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		}).show();
	}

	public static double distance(double lat1, double lon1, double lat2, double lon2) {
		float[] results = new float[3];
		Location.distanceBetween(lat1, lon1, lat2, lon2, results);
		return results[0];
	}

	private static double deg2rad(double deg) {
        return (deg * Math.PI / 180.0);
    }

    private static double rad2deg(double rad) {
        return (rad * 180.0 / Math.PI);
    }
    
    /**
     * calculate new GpsPoint with given base latitude/longitude along given direction
     * 
     * @param latitude
     * @param longitude
     * @param bearing:
     *                                ^ North 0
     *                                |
     *                                |
     *                 West(270)------------> East(90)
     *                                |
     *                                |
     *                                | South (180)
     * @param distanceInKM
     * @return
     */
	public static GpsPoint relativeGpsPoint(double latitude , double longitude , double bearing , double distance ) {	
		double latitudeTo  = deg2rad(latitude);
		double longitudeTo = deg2rad(longitude);
		double bearingTo   = deg2rad(bearing);
	    double angTo       = distance / EARTH_RADIUS_IN_METERS;
	    double latitudeToB = Math.asin(Math.sin(latitudeTo) * Math.cos(angTo) + Math.cos(latitudeTo) * Math.sin(angTo) * Math.cos(bearingTo));
	    double longitudeToB = longitudeTo + Math.atan2(Math.sin(bearingTo) * Math.sin(angTo) * Math.cos(latitudeTo), Math.cos(angTo) - Math.sin(latitudeTo) * Math.sin(latitudeToB));
	    
	    latitudeTo = rad2deg(latitudeToB) ;
	    longitudeTo= rad2deg(longitudeToB);
	    
	    Log.i("Utils","Utils Distance:" + distance(latitude,longitude,latitudeTo,longitudeTo));
		return new GpsPoint( latitudeTo , longitudeTo ) ;
	}
	
}
